package com.applink.sell_proof.builder;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TableRow;
import android.widget.TextView;

import com.applink.sell_proof.R;
import com.applink.sell_proof.model.Product;

public class ProductModelBuilder extends AbstractListBuilder implements
		OnClickListener {

	private int mWidth;
	protected int mSelectedPosition = -1;

	public ProductModelBuilder(Context context, ViewGroup parent) {
		super(context, parent);

		mWidth = mContext.getResources().getDisplayMetrics().widthPixels;
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void buildData() {
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.header_product_model_item: {
			int position = (Integer) v.getTag();
			Product p = (Product) mData.get(position);
			Intent result = new Intent();
			result.putExtra("model_no", p.getModelNo());
			result.putExtra("model_name", p.getModelName());
			result.putExtra("product_id", p.getProductId());
			((Activity) mContext).setResult(Activity.RESULT_OK, result);
			((Activity) mContext).finish();
			break;
		}
		case R.id.table_row_product_model: {

			int position = (Integer) v.getTag();

			int firstItem = mListView.getFirstVisiblePosition()
					- mListView.getHeaderViewsCount();

			int real_position = position - firstItem;

			if (real_position >= 0 && real_position < mListView.getChildCount()) {
				View layout_item = mListView.getChildAt(real_position);

				View menu = layout_item.findViewById(R.id.product_model_menu);

				menu.setVisibility(menu.getVisibility() == View.VISIBLE ? View.GONE
						: View.VISIBLE);

				mSelectedPosition = menu.getVisibility() == View.VISIBLE ? position
						: -1;

				notifyDataChange();
			}
			break;
		}
		default:
			break;
		}

	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater
				.inflate(R.layout.product_model_item, null);

		// get layout of table in an item
		TableRow tableRow = (TableRow) layout
				.findViewById(R.id.table_row_product_model);

		// set OnClick listener on each row
		tableRow.setOnClickListener(this);

		layout.findViewById(R.id.header_product_model_item).setOnClickListener(
				this);

		// set the width for each column in the table
		for (int i = 0; i < tableRow.getChildCount(); i++) {
			View child = tableRow.getChildAt(i);
			child.getLayoutParams().width = mWidth / 2;
		}

		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.table_row_product_model,
				base.findViewById(R.id.table_row_product_model));

		views.put(R.id.product_model_menu,
				base.findViewById(R.id.product_model_menu));

		views.put(R.id.category_name, base.findViewById(R.id.category_name));

		views.put(R.id.model_name, base.findViewById(R.id.model_name));

		views.put(R.id.model_no, base.findViewById(R.id.model_no));

		views.put(R.id.header_product_model_item,
				base.findViewById(R.id.header_product_model_item));

		return views;
	}

	@Override
	public void builConvertViewContentData(int position,
			ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Product p = (Product) mData.get(position);

		String replaceProduct = p.getCatName();

		if (replaceProduct.contains("&amp;")) {
			replaceProduct = replaceProduct.replace("&amp;", " ");
		}
		((TextView) views.get(R.id.category_name)).setText(replaceProduct);

		((TextView) views.get(R.id.model_name)).setText(p.getModelName());
		((TextView) views.get(R.id.model_no)).setText(p.getModelNo());

		views.get(R.id.table_row_product_model).setTag(position);

		views.get(R.id.header_product_model_item).setTag(position);

		views.get(R.id.product_model_menu).setVisibility(
				mSelectedPosition == position ? View.VISIBLE : View.GONE);

	}
}
